---
title: 'Fig.2/3'
author: "Yilin Zhuo"
date: "5/15/2022"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(readstata13)
library(ggplot2)
library(ggpubr)
```

## Figure 2: Plot Police Hour in Whitest/Blackest Neighborhood (20 quantile)

```{r fig2}
# Read data
# Change to your directory
setwd("~/Dropbox/Policing Intensity/Replication/")

hour_df <- read.dta13("Output/city_bw_exposure_hour_20quantile.dta")

# order by hour in whitest neighborhood
hour_df$order_wh_hr <- factor(hour_df$city2, 
                         levels = hour_df$city2[order(hour_df$avg_hour_topwhite)])
# make range plot (on police hour in )
black_white_hr <- ggplot(hour_df, aes(y=order_wh_hr)) +
  geom_linerange(aes(xmin = hour_top_black_lb,
                     xmax = hour_top_black_ub),
                 linetype = 1,size = 1, color="#000000") +
  geom_linerange(aes(xmin = hour_top_white_lb,
                     xmax = hour_top_white_ub),
                 linetype = 1,size = 1, color='#2981A2') +
  geom_point(aes(x=avg_hour_topblack, color = "Black"), size=2) +
  geom_point(aes(x=avg_hour_topwhite, color = "White"), size=2,
             shape = 17) +
  #scale_x_continuous(breaks=seq(0, 0.24, 0.02)) +
  scale_y_discrete(expand = expansion(add = 1 )) + 
  scale_color_manual(values=c('#000000','#2981A2')) + 
  xlab("Police Hour") +
  ylab("") + 
  theme(panel.background = element_rect(fill = NA),
        panel.grid.major = element_line(colour = "#E8E8E8", linetype = "dotted", size = 0.2),
        panel.grid.minor = element_line(colour = "#E8E8E8", linetype = "dotted", size = 0.2),
        aspect.ratio=2,
        axis.text=element_text(size=12),
        axis.title=element_text(size=12),
        legend.position=c(0.85,0.10),
        #legend.position="right", 
        legend.title=element_blank(),legend.background=element_blank()) + 
  guides(color=guide_legend(override.aes=list(shape=c(16,17))))
black_white_hr
ggsave(black_white_hr, file = "Output/cities_bw_nhood_avg_hour.png", 
       dpi = 400, width = 6, height = 6)
```

## Figure 3: Variation of police hour explained by socioeconomics vs. race

```{r fig3}
# set working dir
# Change to your directory
setwd("~/Dropbox/Policing Intensity/Replication/")

# read data
df <- read.dta13("Output/city_abs_race_reg_r_sq_summary.dta")

# create a categorical variable,
# where city is ordered by the level of variable demo_R_sq
df$city_order <- factor(df$city3, levels = df$city3[order(df$demo_R_sq)])

# create a funcction to change the labels to two decimal places 
scaleFUN <- function(x) sprintf("%.2f", x)

# create a range plot
ggplot(df, aes(y=city_order)) +
  geom_linerange(aes(xmin=demo_R_sq,xmax=demo_race_R_sq),linetype=1,size = 0.8,color="#E5E5E5") +
  geom_point(aes(x=demo_R_sq, color = "Socioeconomics, Crime"), size=1.8 ) +
  geom_point(aes(x=demo_race_R_sq, color = "Socioeconomics, Crime, Race"), size=1.8, shape = 17) + 
  #geom_text(aes(label=round(race_R_sq, digits = 3), x=race_R_sq), 
  #          size = 1.8, hjust=1.2, vjust=0, color = '#C71E1D') + 
  scale_x_continuous(breaks=seq(0, 0.30, 0.03), 
                     limits = c(0, 0.30)) +
  scale_y_discrete(expand = expansion(add = 1 )) + 
  scale_color_manual(values=c('#C71E1D','#2981A2')) + 
  xlab("R-squared") +
  ylab("") + 
  theme(panel.background = element_rect(fill = NA),
        panel.grid.major = element_line(colour = "#E8E8E8", linetype = "dotted", size = 0.2),
        panel.grid.minor = element_line(colour = "#E8E8E8", linetype = "dotted", size = 0.2),
        aspect.ratio=1,
        axis.title = element_text(size = 12),
        axis.text = element_text(size = 12),
        #legend.position=c(0.95,0.10),
        legend.position="bottom", 
        legend.title=element_blank(),legend.background=element_blank()) + 
  guides(color=guide_legend(override.aes=list(shape=c(16,17))))

ggsave(file = "Output/R_ranges_cities_absrace.png",dpi = 500, width = 6, height = 6)
  
```


